package com.longxi.tianjintianbao.ui.presenter.houseinspection;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.BaseResponse;
import com.longxi.tianjintianbao.api.response.HttpResult;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.model.houseinspection.YanFang;
import com.longxi.tianjintianbao.ui.activity.houseinspection.AssetsDetailsActivity;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.view.houseinspection.IAssetsCheckAtView;
import com.longxi.tianjintianbao.utils.LogUtils;
import com.longxi.tianjintianbao.utils.MyUtils;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;

import org.json.JSONException;
import org.json.JSONObject;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by cxw on & 2018/9/20
 */
public class AssetsCheckAtPresenter extends BasePresenter<IAssetsCheckAtView> {

    public String state = "正常";

    public AssetsCheckAtPresenter(BaseActivity context) {
        super(context);
    }

    public void initView() {
//        mContext.setTitle("入住验房");
        mContext.setTvLeft("返回");
        mContext.setTvRight("资产详情");
        mContext.mTvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mContext.startActivity(new Intent(mContext, AssetsDetailsActivity.class));
            }
        });
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case PictureConfig.CHOOSE_REQUEST:
                // 图片选择结果回调
                addPicUtils.selectList = PictureSelector.obtainMultipleResult(data);
                gridImageAdapter.setList(addPicUtils.selectList);
                gridImageAdapter.notifyDataSetChanged();
                break;
        }
    }

    public void checkInSave(){

        String explain = getView().getEtExplain().getText().toString().trim();

        if(TextUtils.isEmpty(explain)){
            MyUtils.showToast("请输入情况描述!");
            return;
        }

        String image1 = "";
        String image2 = "";
        String image3 = "";
        for (int i = 0; i < addPicUtils.selectList.size(); i++) {
            if(i == 0){
                image1 = MyUtils.imageToBase64(addPicUtils.selectList.get(i).getCompressPath());
            }else if(i == 1){
                image2 = MyUtils.imageToBase64(addPicUtils.selectList.get(i).getCompressPath());
            }else if(i == 2){
                image3 = MyUtils.imageToBase64(addPicUtils.selectList.get(i).getCompressPath());
            }
        }

        mContext.showWaitingDialog(MyUtils.getString(R.string.load));
        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
            object.put("billid", mContext.getIntent().getStringExtra(AppConst.BILLID));
            object.put("type", mContext.getIntent().getStringExtra(AppConst.TYPE));
            object.put("state", state);
            object.put("explain", explain);
            object.put("initialdegree", "");
            object.put("image1", image1);
            object.put("image2", image2);
            object.put("image3", image3);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().checkInSave(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<BaseResponse>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(BaseResponse result) {
                mContext.hideWaitingDialog();
                if(result.getCode() != 0){
                    MyUtils.showToast(result.getMsg());
                    return;
                }
                MyUtils.showToast("保存成功");

                mContext.setResult(mContext.RESULT_OK);
                mContext.finish();
            }
        });
    }

    public void checkOutSave(){

        String explain = getView().getEtExplain().getText().toString().trim();

        if(TextUtils.isEmpty(explain)){
            MyUtils.showToast("请输入情况描述!");
            return;
        }

        String image1 = "";
        String image2 = "";
        String image3 = "";
        for (int i = 0; i < addPicUtils.selectList.size(); i++) {
            if(i == 0){
                image1 = MyUtils.imageToBase64(addPicUtils.selectList.get(i).getCompressPath());
            }else if(i == 1){
                image2 = MyUtils.imageToBase64(addPicUtils.selectList.get(i).getCompressPath());
            }else if(i == 2){
                image3 = MyUtils.imageToBase64(addPicUtils.selectList.get(i).getCompressPath());
            }
        }

        mContext.showWaitingDialog(MyUtils.getString(R.string.load));
        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
            object.put("billid", mContext.getIntent().getStringExtra(AppConst.BILLID));
            object.put("type", mContext.getIntent().getStringExtra(AppConst.TYPE));
            object.put("state", state);
            object.put("explain", explain);
            object.put("leavedegree", "");
            object.put("image1", image1);
            object.put("image2", image2);
            object.put("image3", image3);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().checkOutSave(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<BaseResponse>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(BaseResponse result) {
                mContext.hideWaitingDialog();
                if(result.getCode() != 0){
                    MyUtils.showToast(result.getMsg());
                    return;
                }
                MyUtils.showToast("保存成功");

                mContext.setResult(mContext.RESULT_OK);
                mContext.finish();
            }
        });
    }
}
